home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / sound / rukc1d.zip / RUCK_SRC.ZIP / X01.C < prev    next >
C/C++ Source or Header  |  1994-02-27  |  9KB  |  304 lines

  1.  
  2. #include <dos.h>
  3. #include <conio.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <stdarg.h>
  8.  
  9. #include "ruckdac.h"
  10.  
  11. /*
  12. X01.c 28-Feb-94 chh
  13. Load & Play VOC/WAV file
  14. removed C7-specific _ all over the place
  15. */
  16.  
  17. /*
  18. The following structures are in ruckdac.h
  19. */
  20.  
  21. #pragma pack(1)  /* must have byte-aligned structures for Ruckus */
  22.  
  23. extern struct DacDataArea pascal DACDATA;
  24.  
  25. struct SysInfoPack SIP;
  26. struct InitPack IP;
  27. struct XitPack XP;
  28. struct LoadPack LP;
  29. struct SetPack SP;
  30. struct SetProPack SPP;
  31. struct PlaybackPack PBP;
  32. struct GetDataPack GDP;
  33. struct DeallocPack DP;
  34.  
  35. #pragma pack()
  36.  
  37. int rez, rez2;      /* result status codes */
  38. char nums[9] = {7}; /* number buffer for _cgets()*/
  39. char filename[81];  /* pathname to load */
  40.  
  41.  
  42. int pick_device(int *devID, int *XMSflag)
  43. {
  44.  
  45.     int td=0;
  46.  
  47.     SIP.Func = SysInfoDac;
  48.     rez = RUCKDAC(&SIP);
  49.     if (rez == 0) {
  50.         printf("CPU is a %u/%u\n",SIP.CPU,SIP.MHz);
  51.  
  52.         printf("\n0. End program");
  53.         printf("\n1. PC speaker at port 42h");
  54.         if (SIP.SD[1].device)
  55.             printf("\n2. LPT-DAC on LPT1, port %xh",SIP.SD[1].Port);
  56.         if (SIP.SD[2].device)
  57.             printf("\n3. Disney Sound Source port %xh",SIP.SD[2].Port);
  58.         if (SIP.SD[3].device)
  59.             printf("\n\n4. AdLib Music Synthesizer Card, port %xh",SIP.SD[3].Port);
  60.         if (SIP.SD[4].device)
  61.             printf("\n5. Sound Blaster, port %xh",SIP.SD[4].Port);
  62.         if (SIP.SD[5].device)
  63.             printf("\n6. Sound Blaster Pro, port %xh",SIP.SD[5].Port);
  64.         if (SIP.SD[4].device)
  65.             printf("\n\n7. Sound Blaster as 5 but use XMS (if applicable)");
  66.         if (SIP.SD[5].device)
  67.             printf("\n8. Sound Blaster Pro as 6 but use XMS (if applicable)\n");
  68.  
  69.         printf("\nSelection: ");
  70.     td = atoi(cgets(nums));
  71.         td--;   /* since devices are numbered 0 to 5 */
  72.  
  73.         if (td > 6) {
  74.             *XMSflag = 1;       /* XMS memory selected with SB */
  75.             td-=2;              /* map to appropriate device */
  76.         }
  77.  
  78.         if ((td >=0) && (td <=5)) { /* validate device selected available */
  79.             if (SIP.SD[td].device == 0)
  80.                 td = -1;
  81.         }
  82.         else
  83.             td = -1;
  84.     }
  85.  
  86.     *devID = td;
  87.     return(rez);
  88. }
  89.  
  90.  
  91. int init_device(int devID)
  92. {
  93.     /*
  94.     Initialize RUCKDAC and device and register ExitDac with _atexit
  95.     The IP.port for devices 0 and 3 are set to 0 for low-rez mode,
  96.     or their respective actual ports for hi-rez mode (0x42 and 0x388)
  97.     */
  98.  
  99.     IP.Func = InitDac;
  100.     IP.DeviceID = devID;
  101.     IP.IOport = SIP.SD[devID].Port;
  102.     IP.IRQline = SIP.SD[devID].IRQ;
  103.     IP.DMAch = SIP.SD[devID].DMA;
  104.  
  105.     if ((devID == 0) || (devID == 3))   /* use low-rez mode for */
  106.         IP.IOport = 0;                  /* PC speaker and Adlib */
  107.  
  108.     rez = RUCKDAC(&IP);                 /* Initialize */
  109.     if (rez == 0) {
  110.  
  111.         XP.Func = AtExitDac;
  112.         rez2 = RUCKDAC(&XP);
  113.         if (rez2 != 0) {
  114.             printf("AtExitDac failed, press Enter to continue");
  115.             getchar();
  116.         }
  117.  
  118.  
  119.         /*
  120.         Increase SB Pro main and vol volumes to max
  121.         */
  122.  
  123.         if (devID == 5) {
  124.             SPP.Func = SetVolMainSBP;
  125.             SPP.Volume = 0x0F0F;
  126.             rez2 = RUCKDAC(&SPP);
  127.             SPP.Func = SetVolVocSBP;
  128.             SPP.VolVoc = 0x0F0F;
  129.             rez2 = RUCKDAC(&SPP);
  130.         }
  131.     }
  132.     return(rez);
  133. }
  134.  
  135.  
  136. int main()
  137. {
  138.  
  139.     int devID=-1, XMSflag = 0;
  140.  
  141.     printf("X01.C - RUCKUS-DAC play of VOC or WAVE file example. [930228]\n");
  142.  
  143.     rez = pick_device(&devID, &XMSflag);
  144.     if (devID >= 0) {
  145.         printf("Initializing devID %u\n",devID);
  146.         rez = init_device(devID);
  147.  
  148.         /*
  149.         The following load and play example source is coded inline here
  150.         to simply readability -- but it's so easy to add things that I just
  151.         kept adding stuff, so take it slow if you don't follow at first
  152.         */
  153.  
  154.         if (rez == 0) {
  155.             /* load file and setup playback parameters */
  156.  
  157.             printf("\nVOC/WAVE filename: ");
  158.             gets(filename);
  159.  
  160.             LP.Func = LoadDac;
  161.             LP.FilenamePtr = filename;
  162.             LP.StartPos = 0L;           /* start at first byte */
  163.             LP.LoadSize = 0L;           /* autoload entire file */
  164.             LP.XMMflag = XMSflag;
  165.             rez = RUCKDAC(&LP);
  166.             if (rez == 0) {
  167.                 /*
  168.                 Immediately after load, but before play (if non-DMA), we can
  169.                 peek into the DAC data to get the file's recorded sample rate
  170.                 */
  171.  
  172.                 printf(" Sample rate: %u\n",DACDATA.SampleRate);
  173.                 printf(" File format: ");
  174.                 if (DACDATA.Stereo != 0)
  175.                     printf("stereo ");
  176.                 else
  177.                     printf("mono ");
  178.                 if (DACDATA.Type == 1)
  179.                     printf("VOC\n");
  180.                 else
  181.                     printf("WAV\n");
  182.  
  183.                 /*
  184.                 Data is loaded, if device is a Sound Blaster use DMA at
  185.                 file sample rate else set rate to either file sample rate or,
  186.                 if > 11025 (most ATs can handle 11kHz in non-DMA mode) then
  187.                 set to 8000 Hz
  188.                 */
  189.  
  190.                 PBP.Func = PlayDac;
  191.                 if (devID >= 4)
  192.                     PBP.Mode = 2;
  193.                 else {
  194.                     PBP.Mode = 1;
  195.  
  196.                     /*
  197.                     Non-DMA mode needs to be set to a specific playback rate.
  198.  
  199.                     To play hal.voc using PCSPKR1 (the hi-rez mode) set
  200.                     SP.IntRate=8463. This results in upsample rate of 17045Hz
  201.                     */
  202.  
  203.                     if (DACDATA.SampleRate < 11025)
  204.                         SP.IntRate = DACDATA.SampleRate;
  205.                     else
  206.                         SP.IntRate = 8000;
  207.  
  208.                     SP.Func = SetIntRateDac;
  209.                     rez = RUCKDAC(&SP); /* set the playback rate */
  210.                     /* should check for errors in production code*/
  211.                 }
  212.  
  213.                 /*
  214.                 Load the file to DOS RAM or XMS
  215.                 */
  216.  
  217.                 if (LP.XMMflag == 0) {
  218.                     PBP.XMMhandle = 0;
  219.                     PBP.LoadPtr = LP.LoadPtr;
  220.                 }
  221.                 else {
  222.                     PBP.XMMhandle = LP.XMMhandle;
  223.                     PBP.LoadPtr = NULL;
  224.                 }
  225.                 rez = RUCKDAC(&PBP);
  226.  
  227.                 /*
  228.                 Playing in the background, wait until down or key pressed.
  229.                 To check if data done playing, read DACDATA.End. If non-zero
  230.                 then done playing. Note that once play has actually begun,
  231.                 the DACDATA.SampleRate is set to the actual playback rate,
  232.                 not necessarily the same as the rate at load time.
  233.                 */
  234.  
  235.                 if (rez == 0) {
  236.                     printf("   Device ID: %u\n",devID);
  237.                     printf("   Play rate: %u Hz\n",DACDATA.SampleRate);
  238.  
  239.                     if (LP.XMMflag == 0) {
  240.                         printf(" Memory type: DOS\n");
  241.                         printf("K bytes left: %u\n",DACDATA.MemDOS);
  242.                         printf("K bytes used: %u\n",DACDATA.MemUsed);
  243.                         printf("Load address: %Fp\n",LP.LoadPtr);
  244.                     }
  245.                     else {
  246.                         printf(" Memory type: XMS\n");
  247.                         printf("K bytes left: %u\n",DACDATA.MemXMM);
  248.                         printf("K bytes used: %u\n",DACDATA.MemUsed);
  249.                         printf("  XMS handle: %u\n",LP.XMMhandle);
  250.                     }
  251.  
  252.  
  253.                     GDP.Func = GetBytePosDac;
  254.                     do {
  255.                         rez = RUCKDAC(&GDP);
  256.                         if (GDP.BytePos != 0)
  257.                             printf("Current byte: %LX\r",GDP.BytePos);
  258.                     }
  259.